package com.classeven.downhill.object;

import org.andengine.entity.modifier.MoveXModifier;

import com.classeven.downhill.define.iDefine_Object;
import com.classeven.downhill.define.iDefines;
import com.classeven.downhill.define.iGameDefines;
import com.classeven.downhill.element.ASprite;
import com.classeven.downhill.engine.Anchor;
import com.classeven.downhill.engine.GameEngine;
import com.classeven.downhill.engine.Utils;

public class Obstacle_Element {

	GameEngine engine;
	public ASprite obs;
	Anchor anchor;
	float scale = 1f;
	
	public Obstacle_Element(GameEngine engine, Anchor anchor, float startPos, float distanceWithTop, int Obstacle)
	{
		this.anchor = anchor;
		this.engine = engine;
		float y = Utils.GetRatio(startPos);
		
		if(Obstacle == iGameDefines.OBS_TREE)
			obs = new ASprite(engine, iGameDefines.SPRITE_OBS, iGameDefines.OBS_TREE);
		else
			obs = new ASprite(engine, iGameDefines.SPRITE_OBS, iGameDefines.OBS_STONE);
		
//		obs.theSprite().setScale(1f);
		obs.SetPos(Utils.GetAnchorX(obs.theSprite().getWidth(), anchor), y + Utils.GetRatio(distanceWithTop));
	}
	
	public void init()
	{
		obs.theSprite().setY( GameEngine.CAMERA_HEIGHT + Utils.GetRatio(200));
	}
	
	public void update(float speed)
	{
		if(anchor == iDefine_Object.ancor[iDefine_Object.left_ancor])
		{
			
			if(obs.GetY() <= GameEngine.CAMERA_HEIGHT)
			{
				
				obs.theSprite().setX(obs.GetX() + speed /9);
				obs.theSprite().setScale(scale -=0.0009f);
			}
			
			obs.theSprite().setY(obs.GetY() - speed);
			
		}
		else if(anchor == iDefine_Object.ancor[iDefine_Object.right_ancor])
		{
			if(obs.GetY() <= GameEngine.CAMERA_HEIGHT)
			{
				
				obs.theSprite().setX(obs.GetX() - speed /9);
				obs.theSprite().setScale(scale -=0.0009f);
			}
			
			obs.theSprite().setY(obs.GetY() - speed);
			
		}
		else if(anchor == iDefine_Object.ancor[iDefine_Object.Mid_1_ancor])
		{
			if(obs.GetY() <= GameEngine.CAMERA_HEIGHT)
			{
				obs.theSprite().setX(obs.GetX() + speed /20);
				obs.theSprite().setScale(scale -=0.0009f);
			}
			
			obs.theSprite().setY(obs.GetY() - speed);
			
		}
		
		else
		{
			if(obs.GetY() <= GameEngine.CAMERA_HEIGHT)
			{
				obs.theSprite().setX(obs.GetX() - speed /20);
				obs.theSprite().setScale(scale -=0.0009f);
			}
			
			obs.theSprite().setY(obs.GetY() - speed);
		}
//		Utils.TRACE("Munggah " + obs.theSprite().getWidth() +", "+  obs.theSprite().getWidth());
		
		if(obs.GetY() <= - obs.theSprite().getHeight())
		{
			anchor = iDefine_Object.ancor[Utils.GetRandomInRange(0, iDefine_Object.ancor.length - 1)];
			obs.SetPos(Utils.GetAnchorX(obs.theSprite().getWidth(),  anchor),
						GameEngine.CAMERA_HEIGHT + Utils.GetRatio(iDefine_Object.distance[Utils.GetRandomInRange(0, iDefine_Object.distance.length - 1)]));
			setScale();
		}
	}
	
	public ASprite getObs()
	{
		return obs;
	}

	public void  setScale()
	{
		obs.theSprite().setScale(1f);
		scale =1;
		obs.theSprite().setX(Utils.GetAnchorX(obs.theSprite().getWidth(), anchor));
	}
	

	
}
